package item

import (
	"sort"
)

type ItemSlice []*SettingItem

func (p ItemSlice) Len() int           { return len(p) }
func (p ItemSlice) Less(i, j int) bool { return p[i].Level < p[j].Level }
func (p ItemSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p ItemSlice) Sort()              { sort.Sort(p) }
func (l ItemSlice) Append(t *SettingItem) ItemSlice {
	ll := append(l, t)
	return ll
}
func (l ItemSlice) Remove(index int) ItemSlice {
	var ll ItemSlice
	switch index {
	case 0:
		ll = l[1:]
	case len(l) - 1:
		ll = l[0 : len(l)-1]
	default:
		ll = append(l[:index], l[index+1:]...)
	}
	ll.Sort()
	return ll
}

func (l ItemSlice) SearchGeqLevel(level int) int {
	index := sort.Search(l.Len(), func(i int) bool { return l[i].Level >= level })
	//fmt.Println(index, l[index].Level, level)
	if index < l.Len() {
		return index
	} else {
		return -1
	}
}
func (l ItemSlice) SearchLeqLevel(level int) int {
	index := sort.Search(l.Len(), func(i int) bool { return l[i].Level <= level })
	//fmt.Println(index, l[index].Level, level)
	if index < l.Len() {
		return index
	} else {
		return -1
	}
}
